<template>
  <el-dialog title="班次管理" :visible.sync="dialogvisible" width="750px" append-to-body class="my-dialog"
             :close-on-click-modal="false" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="班次名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入班次名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="!param">
<!--          <el-form-item label="所属执勤点">-->
<!--            <span slot="label" style="display:inline-block;">-->
<!--              所属执勤点-->
<!--              <el-tooltip effect="light" content="不填默认全站通用" placement="top">-->
<!--                <i class='el-icon-warning-outline'/>-->
<!--              </el-tooltip>-->
<!--            </span>-->
<!--            <el-select-->
<!--              :style="{width: '100%'}"-->
<!--              v-model="form.pointsId"-->
<!--              filterable-->
<!--              remote-->
<!--              clearable-->
<!--              size="small"-->
<!--              reserve-keyword-->
<!--              placeholder="所属执勤点"-->
<!--              :remote-method="remoteMethod_points"-->
<!--              :loading="loading_points">-->
<!--              <el-option-->
<!--                v-for="item in points_list"-->
<!--                :key="item.id"-->
<!--                :label="item.name || '未知'"-->
<!--                :value="item.id">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
        </el-col>
<!--        <el-col :span="12">-->
<!--          <el-form-item label="班次负责人">-->
<!--            <el-select-->
<!--              :style="{width: '100%'}"-->
<!--              v-model="form.userId"-->
<!--              filterable-->
<!--              remote-->
<!--              size="small"-->
<!--              reserve-keyword-->
<!--              placeholder="请输入班次负责人关键词"-->
<!--              :remote-method="remoteMethod_user"-->
<!--              :loading="loading_user">-->
<!--              <el-option-->
<!--                v-for="item in user_list"-->
<!--                :key="item.userId"-->
<!--                :label="item.nickName || '未知'"-->
<!--                :value="item.userId">-->
<!--              </el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="12">
          <el-form-item label="上下班时间">
            <el-button @click="addTimeRule" size="small" plain :disabled="timelist.length<2?false:true">新增</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="timelist.length" class="time-header">
          <div class="workTime-body" v-for="(item, index) in timelist">
            <p class="workTime-title">
              <span class="workTime-title-name">第{{ index + 1 }}次上下班</span>
              <span class="el-icon-error workTime-title-icon" v-if="index" @click="detelLine(index)"></span>
            </p>
            <div class="workTime-time">
              <div class="workTime-time-list">
                <div class="workTime-time-list-l">
                  上班时间
                  <el-time-picker
                    size="small"
                    v-model="item.start_time"
                    format="HH:mm"
                    value-format="HH:mm"
                    placeholder="选择时间"
                    @change="changeTime(index, 'start')">
                  </el-time-picker>
                  <i class="workTime-time-list-i" v-if="!item.is_today_start">次日</i>
                </div>
                <div class="workTime-time-list-r">
                  <el-checkbox v-model="item.start_clock" :disabled="!index">打卡</el-checkbox>
                </div>
              </div>
              <div class="workTime-time-list">
                <div class="workTime-time-list-l">
                  上班可打卡时间
                  <el-time-picker
                    size="small"
                    v-model="item.startComeTime"
                    format="HH:mm"
                    value-format="HH:mm"
                    placeholder="选择时间">
                  </el-time-picker>
                  至
                  <el-time-picker
                    v-model="item.endComeTime"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间">
                  </el-time-picker>
                </div>
              </div>
              <div class="workTime-time-list">
                <div class="workTime-time-list-l">
                  下班时间
                  <el-time-picker
                    v-model="item.end_time"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间"
                    @change="changeTime(index, 'end')">
                  </el-time-picker>
                  <i class="workTime-time-list-i" v-if="!item.is_today_end">次日</i>
                </div>
                <div class="workTime-time-list-r">
                  <el-checkbox v-model="item.end_clock">打卡</el-checkbox>
                </div>
              </div>
              <div class="workTime-time-list">
                <div class="workTime-time-list-l">
                  下班可打卡时间
                  <el-time-picker
                    v-model="item.startOutTime"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间">
                  </el-time-picker>
                  至
                  <el-time-picker
                    v-model="item.endOutTime"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间">
                  </el-time-picker>
                </div>
              </div>
            </div>
          </div>
        </el-col>
        <!--        <el-col :span="24">-->
        <!--          <div class="abnormal-time">-->
        <!--            <el-checkbox v-model="checked">晚到、早走几分钟不计为异常</el-checkbox>-->
        <!--            <div class="abnormal-time-list">-->
        <!--              上班最多可晚到：-->
        <!--              <div class="time-box">-->
        <!--                <el-cascader-->
        <!--                  :options="props"-->
        <!--                  placeholder="选择时间"-->
        <!--                  v-model="toWork"-->
        <!--                  ref="startTime"-->
        <!--                  style="width:200px"-->
        <!--                  size="small"-->
        <!--                  clearable-->
        <!--                  @change="handleDephours()"-->
        <!--                ></el-cascader>-->
        <!--              </div>-->
        <!--              晚到{{ toWorkName }}不算迟到-->
        <!--            </div>-->
        <!--            <div class="abnormal-time-list">-->
        <!--              下班最多可早走：-->
        <!--              <div class="time-box">-->
        <!--                <el-cascader-->
        <!--                  :options="props"-->
        <!--                  placeholder="选择时间"-->
        <!--                  v-model="afterWork"-->
        <!--                  ref="endTime"-->
        <!--                  clearable-->
        <!--                  size="small"-->
        <!--                  style="width:200px"-->
        <!--                  @change="handleDepMini()"-->
        <!--                ></el-cascader>-->
        <!--              </div>-->
        <!--              早走{{ afterWorkName }}不算早退-->
        <!--            </div>-->
        <!--          </div>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <div class="rest-time">
            <div class="rest-time-title">
              休息时间
              <el-button type="text" @click="addRestTime()">添加</el-button>
            </div>
            <div class="rest-time-list">
              <div class="rest-time-line" v-for="(item, index) in restList" :key="index">
                <div class="mr10">
                  休息开始
                  <el-time-picker
                    style="width: 120px"
                    v-model="item.start"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间"
                    @change="changeRestTime(index, 'start')">
                  </el-time-picker>
                </div>
                <div>
                  休息结束
                  <el-time-picker
                    style="width: 120px"
                    v-model="item.end"
                    format="HH:mm"
                    size="small"
                    value-format="HH:mm"
                    placeholder="选择时间"
                    @change="changeRestTime(index, 'end')">
                  </el-time-picker>
                </div>
                <span class="el-icon-circle-close ml5" @click="detelFn(index)"></span>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="abnormal-time">
            <div class="abnormal-time-list">
              严重迟到分钟数：
              <div class="time-box">
                <el-input size="small" v-model="form.late" placeholder="请输入时长" style="width:200px"
                          type="numbers"></el-input>
              </div>
            </div>
            <div class="abnormal-time-list">
              旷工迟到分钟数：
              <div class="time-box">
                <el-input size="small" v-model="form.abstained" placeholder="请输入时长" style="width:200px"
                          type="numbers"></el-input>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="daytime">
            <p class="daytime-tips"><em>*</em>班次工时<span>（考勤统计工作时长及请假出差外出统计，会以此时间为准）</span></p>
            <div class="daytime-count">
              合计工作时长{{ restTime[0] }}小时{{ restTime[1] }}分钟，计为1
              <!--              <el-input v-model="form.workTime" placeholder="请输入" style="width:100px" type="numbers"></el-input>-->
              天出勤
            </div>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" size="small" plain>确 定</el-button>
      <el-button @click="cancel" size="small" plain>取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addSignCate, updateSignCate, getSignCate} from "@/api/system/sign_category";
import {listUser} from '@/api/system/user'
import {
  listStagnationPoint,
  getStagnationPoint
} from "@/api/system/stagnation_point";

export default {
  props: ['visible', 'param', 'paramId', 'type'], // type allIn 通用
  name: "index",
  data() {
    return {
      dialogvisible: false,
      form: {},
      value1: '',
      checked: false,
      props: [],
      propsAfter: {
        lazy: true,
        label: "name",
        value: "id",
        lazyLoad: this.lazyLoadAfter,
      },
      toWork: [], //
      afterWork: [],
      toWorkName: '',
      afterWorkName: '',
      loading_user: false,
      user_list: [],
      timelist: [],
      defultTime: {
        start_time: '', // 上班时间
        end_time: '', // 下班时间
        start_clock: true, // 上班可打卡
        end_clock: true, //下班可打卡
        is_today_start: true, // 上班时间是否是今天
        is_today_end: true, // 下班时间是否是今天
        startComeTime: '', // 上班打卡时间开始
        endComeTime: '',// 上班可打卡时间结束
        startOutTime: '', // 下班可打卡时间开始
        endOutTime: '', // 下班可打卡时间结束
      },
      rules: {
        name: [
          {required: true, message: "班次名称不能为空", trigger: "blur"}
        ]
      },
      loading_points: false,
      points_list: [],
      restList: [],
      restTime: [0, 0],
      workTimeCount: 0, // 班次一共工作总时长
    }
  },
  watch: {
    visible() {
      this.dialogvisible = this.visible
      let that = this
      if (this.visible) {
        console.log('执勤点信息', this.param)
        this.getTime()
        // param 执勤点信息
        if (this.param) {
          this.form.projectId = this.param.projectId // 项目id
          this.form.pointsId = this.param.id.toString() // 驻点id
          this.getDept()
        }
        if (this.paramId) {
          this.getDetail()
        } else {
          this.addTimeRule()
        }
      } else {
        this.reset()
      }
    }
  },
  created() {
  },
  methods: {
    getDetail() {
      getSignCate(this.paramId).then(res => {
        let time_ = []
        time_.push({
          start_time: res.data.comeTime1, // 上班时间
          end_time: res.data.outTime1, // 下班时间
          start_clock: true, // 上班可打卡
          end_clock: res.data.isOutSign1 ? true : false, //下班可打卡
          is_today_start: true, // 上班时间是否是今天
          is_today_end: res.data.outNext1 ? false : true, // 下班时间是否是今天
          startComeTime: this.$deepCopy(res.data.startComeTime1), // 上班打卡时间开始
          endComeTime: this.$deepCopy(res.data.endComeTime1),// 上班可打卡时间结束
          startOutTime: res.data.startOutTime1, // 下班可打卡时间开始
          endOutTime: res.data.endOutTime1, // 下班可打卡时间结束
        })

        if (res.data.startTime2) {
          time_.push({
            start_time: res.data.comeTime2, // 上班时间
            end_time: res.data.outTime2, // 下班时间
            start_clock: res.data.isStartSign2 ? true : false, // 上班可打卡
            end_clock: res.data.isEndSign2 ? true : false, //下班可打卡
            is_today_start: res.data.comeNext2 ? false : true, // 上班时间是否是今天
            is_today_end: res.data.outNext2 ? false : true, // 下班时间是否时间天
            startComeTime: res.data.startComeTime2, // 上班打卡时间开始
            endComeTime: res.data.endComeTime2,// 上班可打卡时间结束
            startOutTime: res.data.startOutTime2, // 下班可打卡时间开始
            endOutTime: res.data.endOutTime2, // 下班可打卡时间结束
          })
        }
        this.timelist = time_
        res.data.late = res.data.late || ''
        res.data.abstained = res.data.abstained || ''
        // if (res.data.forgiveStart) {
        //   // 上班迟到允许时间(分钟)
        //   this.toWork = [parseInt(res.data.forgiveStart / 60), res.data.forgiveStart % 60]
        //   this.toWorkName = `${parseInt(res.data.forgiveStart / 60)}小时${res.data.forgiveStart % 60}分钟`
        // }
        // if (res.data.forgiveEnd) {
        //   // 下班早退允许时间（分钟）
        //   this.afterWork = [parseInt(res.data.forgiveEnd / 60), res.data.forgiveEnd % 60]
        //   this.afterWorkName = `${parseInt(res.data.forgiveEnd / 60)}小时${res.data.forgiveEnd % 60}分钟`
        // }
        this.restList = res.data.rest?JSON.parse(res.data.rest):[]
        res.data.pointsId = res.data.pointsId?res.data.pointsId:''
        this.form = res.data
        this.changeRestTime()
        if (res.data.pointsId) {
          this.getDept()
        }
        this.remoteMethod(res.data.userId)
      })
    },
    // 时间判断
    changeTime(index, type) {
      // const currentYear = new Date().getFullYear();
      let item = JSON.parse(JSON.stringify(this.timelist[index]))
      let time_ = JSON.parse(JSON.stringify(this.timelist))
      if (index) {
        // 根据上一组下班时间进行判定
        // 下班时间为当日
        if (type == 'start') {
          item.startComeTime = item.start_time
          item.endComeTime = item.start_time
          console.log('item==>', item)
          if (time_[index - 1].end_time) {
            let date1 = new Date('2024-01-01 ' + item.start_time); // 上班时间
            let date2 = new Date('2024-01-01 ' + time_[index - 1].end_time); // 上组下班时间
            if (time_[index - 1].is_today_end) {
              if (date1 > date2) {
                // 上班 大于 下班时间，当天
                item.is_today_start = true
              } else {
                // 判定隔天
                item.is_today_start = false
              }
            } else {
              if (date1 > date2) {
                // 上班 大于 下班时间，当天
                item.is_today_start = false
              } else {
                // 判定隔天
                this.$message.error('请按时间顺序设置时间点')
                item.start_time = ''
              }
            }
          } else {
            this.$message.error('请按时间顺序设置时间点')
            item.start_time = ''
          }
        }
        // 下班时间为次日
        if (type == 'end') {
          item.startOutTime = item.end_time
          item.endOutTime = item.end_time
          if (item.start_time) {
            let date1 = new Date('2024-01-01 ' + item.start_time); // 上班时间
            let date2 = new Date('2024-01-01 ' + item.end_time); // 上组下班时间
            if (item.is_today_start) {
              if (date1 < date2) {
                // 上班时间 小于 下班时间，当天
                item.is_today_end = true
              } else {
                // 时间相同,判定隔天
                item.is_today_end = false
              }
            } else {
              if (date1 < date2) {
                // 上班时间 小于 下班时间，当天
                item.is_today_end = false
              } else {
                // 时间相同,判定隔天
                this.$message.error('请按时间顺序设置时间点')
                item.end_time = ''
              }
            }
          } else {
            this.$message.error('请按时间顺序设置时间点')
            item.end_time = ''
          }
        }
      } else {
        if (type == 'start') {
          item.startComeTime = item.start_time
          item.endComeTime = item.start_time
        } else {
          item.startOutTime = item.end_time
          item.endOutTime = item.end_time
        }

        if (!item.start_time && type == 'end') {
          this.$message.error('请按时间顺序设置时间点')
          item.end_time = ''
        }
        if (item.start_time && item.end_time) {
          let date1 = new Date('2024-01-01 ' + item.start_time); // 上班时间
          let date2 = new Date('2024-01-01 ' + item.end_time); // 下班时间
          if (item.start_time && item.end_time) {
            if (date1 < date2) {
              // 上班时间 小于 下班时间，当天
              item.is_today_start = true
              item.is_today_end = true
            } else {
              // 时间相同, 隔天
              item.is_today_start = true
              item.is_today_end = false
            }
          }
        }
      }
      this.$set(this.timelist, index, item)
      this.changeRestTime()
    },
    addTimeRule() {
      this.timelist.push(JSON.parse(JSON.stringify(this.defultTime)))
    },
    detelLine(index) {
      this.timelist.splice(index, 1)
    },
    // 表单重置
    reset() {
      this.form = {
        name: '', // 名称
        deptId: '', // 组织id
        projectId: '', // 项目id
        pointsId: 0, // 驻点id
        userId: '', // 负责人

        comeTime1: '', // 上班时间1
        outTime1: '', // 下班时间1
        startComeTime1: '', // 上班允许打卡时间1。起始
        endComeTime1: '', // 上班允许打卡时间1。结束
        startOutTime1: '', // 下班班可打卡时间开始1。起始
        endOutTime1: '', // 下班可打卡时间结束1。结束
        outNext1: '', // 下班时间1是否第二天 0 否 1 是 outNext1
        isOutSign1: '', // 是否需要打卡下班

        comeTime2: '', // 上班时间2
        outTime2: '', // 下班时间2
        startComeTime2: '', // 上班允许打卡时间2，起始
        endComeTime2: '', // 上班允许打卡时间2，结束
        comeNext2: '',// 是否是第二天
        isComeSign2: '', // 上班是否打卡
        startOutTime2: '', // 下班允许打卡时间2，起始
        endOutTime2: '', // 下班允许打卡时间2，结束
        outNext2: '', // 下班时间2是否第二天
        isOutSign2: '', // 是否打卡

        forgiveStart: '', // 上班迟到允许时间(分钟)
        forgiveEnd: '', // 下班早退允许时间（分钟）
        late: '', // 迟到时间（分钟）
        abstained: '', //  旷工时间（分钟）

        isEndSign1: '', // 是否下班要打卡1 isOutSign1
        isStartSign2: '', // 是否上班打卡2 isComeSign2
        isEndSign2: '', // 是否下班打卡2 isOutSign2
        workTime: 1, //  一天工时
        rest: '' // 休息时间json
      };
      this.resetForm("form");
      this.timelist = []
      this.user_list = []
      this.toWork = []
      this.afterWork = []
      this.toWorkName = ''
      this.afterWorkName = ''
      this.loading_points = false
      this.points_list = []
      this.restList = []
      this.restTime = [0, 0]
      this.workTimeCount = 0 // 班次一共工作总时长
    },
    submitForm() {
      // toWork 上班晚到分钟数 afterWork 下班早退分钟数
      let that = this
      this.$refs["form"].validate(valid => {
        if (valid) {
          let time_ = JSON.parse(JSON.stringify(that.timelist))
          let data_ = JSON.parse(JSON.stringify(that.form)) // 需要提交的字段
          let toWorkTime = this.toWork.length ? this.toWork[0] * 60 + this.toWork[1] : 0
          let afterWorkTime = this.afterWork.length ? this.afterWork[0] * 60 + this.afterWork[1] : 0

          for (let index in time_) {
            let item = time_[index]
            if (!item.start_time || !item.end_time) {
              this.$message.error('填写上下班时间段')
              return
            }
            let currentDate_to = new Date('2024-01-01 ' + item.start_time); // 上班时间
            let currentDate_after = new Date('2024-01-01 ' + item.end_time); // 上班时间
            currentDate_to.setMinutes(currentDate_to.getMinutes() + toWorkTime);
            currentDate_after.setMinutes(currentDate_after.getMinutes() - afterWorkTime);
            if (index == '0') {
              // if (toWorkTime) {
              //   data_.startSign1 = this.newDate(currentDate_to.getTime()) // 上班允许晚到时间
              // }
              // if (afterWorkTime) {
              //   data_.endSign1 = this.newDate(currentDate_after.getTime()) // 上班允许晚到时间
              // }
              data_.comeTime1 = item.start_time // 上班时间
              data_.outTime1 = item.end_time // 下班时间
              data_.startComeTime1 = item.startComeTime // 上班打卡时间开始
              data_.endComeTime1 = item.endComeTime // 上班可打卡时间结束
              data_.startOutTime1 = item.startOutTime // 下班可打卡时间开始
              data_.endOutTime1 = item.endOutTime // 下班可打卡时间结束
              data_.outNext1 = item.is_today_end ? 0 : 1 // 下班时间1是否第二天 0 否 1 是
              data_.isOutSign1 = item.end_clock ? 1 : 0 // 下班是否需要打卡
            } else {
              // if (toWorkTime) {
              //   data_.startSign2 = this.newDate(currentDate_to.getTime()) // 上班允许晚到时间
              // }
              // if (afterWorkTime) {
              //   data_.endSign2 = this.newDate(currentDate_after.getTime()) // 上班允许晚到时间
              // } else {
              //
              // }
              data_.comeTime2 = item.start_time // 上班时间
              data_.outTime2 = item.end_time // 下班时间
              data_.startComeTime2 = item.startComeTime // 上班打卡时间开始
              data_.endComeTime2 = item.endComeTime // 上班可打卡时间结束
              data_.startOutTime2 = item.startOutTime // 下班可打卡时间开始
              data_.endOutTime2 = item.endOutTime // 下班可打卡时间结束
              data_.comeNext2 = item.is_today_start ? 0 : 1 // 下班时间1是否第二天 0 否 1 是
              data_.outNext2 = item.is_today_end ? 0 : 1 // 下班时间1是否第二天 0 否 1 是
              data_.isComeSign2 = item.start_clock ? 1 : 0 // 下班是否需要打卡
              data_.isOutSign2 = item.end_clock ? 1 : 0 // 下班是否需要打卡
            }
          }
          data_.forgiveStart = toWorkTime // 上班迟到允许时间(分钟)
          data_.forgiveEnd = afterWorkTime // 下班早退允许时间（分钟）
          data_.late = data_.late || 0 // 迟到时间（分钟）
          data_.abstained = data_.abstained || 0 //  旷工时间（分钟）
          data_.workTime = that.workTimeCount  // 一天工时
          data_.rest = JSON.stringify(this.restList)  // 休息时间json
          if (this.form.pointsId) {
            for (let item of this.points_list) {
              if (item.id == this.form.pointsId) {
                this.form.projectId = item.projectId
              }
            }
          } else {
            this.form.pointsId = 0
            this.form.projectId = 0
          }
          console.log('data_:', data_)
          if (data_.id != undefined) {
            updateSignCate(data_).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('getList')
              this.cancel();
            });
          } else {
            addSignCate(data_).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('getList')
              this.cancel()
            });
          }
        }
      });
    },
    newDate(time) {
      let date = new Date(time)
      // let y = date.getFullYear()
      // let m = date.getMonth() + 1
      // m = m < 10 ? '0' + m : m
      // let d = date.getDate()
      // d = d < 10 ? '0' + d : d
      let h = date.getHours()
      h = h < 10 ? '0' + h : h
      let minute = date.getMinutes()
      minute = minute < 10 ? '0' + minute : minute
      // let s = date.getSeconds()
      // s = s < 10 ? '0' + s : s
      // return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s
      return h + ':' + minute
    },
    cancel() {
      this.$emit('update:visible', false)
    },
    getTime() {
      let hours_ = []
      for (let i = 0; i < 24; i++) {
        let minis_ = {
          value: i,
          label: `${i}小时`,
          children: []
        }
        let children_ = []
        for (let i = 0; i < 60; i++) {
          children_.push({
            value: i,
            label: `${i}分钟`,
            leaf: true
          })
        }
        minis_.children = children_
        hours_.push(minis_)
      }
      this.props = hours_
    },
    // 地址变化徽调
    handleDephours(option) {
      if (this.$refs["startTime"].getCheckedNodes()[0]) {
        let name_ = this.$refs["startTime"].getCheckedNodes()[0].pathLabels;
        this.toWorkName = name_.join('')
      } else {
        this.toWorkName = ''
      }
    },
    handleDepMini(option) {
      if (this.$refs["endTime"].getCheckedNodes()[0]) {
        let name_ = this.$refs["endTime"].getCheckedNodes()[0].pathLabels;
        this.afterWorkName = name_.join('')
      } else {
        this.afterWorkName = ''
      }
    },
    remoteMethod_points(query) {
      this.loading_points = true
      listStagnationPoint({
        pageNum: 1,
        pageSize: 10,
        name: query
      }).then(response => {
        this.points_list = response.rows;
        this.loading_points = false
      });
    },
    getDept() {
      getStagnationPoint(this.form.pointsId).then(res => {
        // res.data.id = parseInt(res.data.detail.id)
        this.points_list.push(res.data.detail)
      })
    },
    // 负责人搜素
    // remoteMethod_user(query) {
    //   this.loading_user = true
    //   listUser({
    //     pageNum: 1,
    //     pageSize: 10,
    //     nickName: query
    //   }).then(response => {
    //     this.user_list = response.rows;
    //     this.loading_user = false
    //   });
    // },
    // // 负责人填充
    // remoteMethod(query) {
    //   listUser({
    //     pageNum: 1,
    //     pageSize: 10,
    //     userId: query
    //   }).then(response => {
    //     this.user_list = response.rows;
    //   });
    // },
    countRestTime() {
      // 计算当天请假时间
      let date1 = new Date();
      let date2 = new Date();

      let diffInMilliseconds = Math.abs(date2 - date1); // 获取两个日期对象之间的毫秒数差值（正负都包括）
      let minutesDiff = Math.floor((diffInMilliseconds / (60 * 1000))); // 分钟
      let minutesDiff_m = minutesDiff % 60
      console.log('minutesDiff_m:', minutesDiff, minutesDiff_m)
      let number_ = parseFloat(`${Math.floor(minutesDiff / 60)}.${minutesDiff % 60}`)
      console.log('number_:', number_)
    },
    addRestTime() {
      this.restList.push({
        start: '',
        end: '',
        duration: 0 // 分钟
      })
    },
    detelFn(index){
      this.restList.splice(index, 1)
    },
    changeRestTime() {
      let countTime = 0
      let work_ = 0
      let today_ = this.formatDate(0) // 今天
      let day_ = this.formatDate(1) // 明天
      for (let item of this.restList) {
        if (item.start && item.end) {
          let date1 = new Date(`${today_} ${item.start}:00`)
          let date2 = new Date(`${today_} ${item.end}:00`)
          if (date1 > date2) {

            date2 = new Date(`${day_} ${item.end}:00`)
            console.log('结束时间是第二天的时间:', day_)
          }
          let diffInMilliseconds = Math.abs(date2 - date1); // 获取两个日期对象之间的毫秒数差值
          countTime = countTime + Math.floor((diffInMilliseconds / (60 * 1000))); // 一共几分钟
        }
      }
      // let house_ = Math.floor(countTime / 60)
      // let minutesDiff_m = countTime % 60 // 余几分钟
      // console.log('休息时间一共:', `${countTime}分钟，转换${house_}小时${minutesDiff_m}分钟`)
      // console.log('timelist==>', this.timelist)// start_time end_time
      // is_today_end
      // is_today_start
      for (let timeCount of this.timelist) {
        if (timeCount.start_time && timeCount.end_time) {
          let date1 = new Date(`${today_} ${timeCount.start_time}:00`)
          let date2 = new Date(`${today_} ${timeCount.end_time}:00`)
          if (!timeCount.is_today_start) {
            date1 = new Date(`${day_} ${timeCount.start_time}:00`)
          }
          if (!timeCount.is_today_end) {
            date2 = new Date(`${day_} ${timeCount.end_time}:00`)
          }
          let workTime = Math.abs(date2 - date1); // 获取两个日期对象之间的毫秒数差值
          work_ = work_ + Math.floor((workTime / (60 * 1000)))
        }
      }
      // let work_house = Math.floor(work_ / 60)
      // let work_minutes = work_ % 60 // 余几分钟
      // console.log('工作时间一共:', `${work_}分钟，转换${work_house}小时${work_minutes}分钟`)
      // this.restTime = [house_, minutesDiff_m]
      if (work_) {
        let day_work_time = work_ - countTime// 一天工作时长
        let day_house = Math.floor(day_work_time / 60)
        let day_minutes = day_work_time % 60 // 余几分钟
        console.log('工作时间扣除休息一共:', `${day_work_time}分钟，转换${day_house}小时${day_minutes}分钟`)
        this.restTime = [day_house, day_minutes]
        this.workTimeCount = day_work_time
      }
      // workTimeCount: 0, // 班次一共工作总时长
      //   restTimeCount: 0 // 休息总时长
    },
    formatDate(index) {
      // 计算日期相关值
      let time = new Date();
      let targetday_milliseconds = time.getTime() + 1000 * 60 * 60 * 24 * index;
      time.setTime(targetday_milliseconds);
      let Y = time.getFullYear();
      let M = time.getMonth() + 1;
      let D = time.getDate();
      return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D);
    },
  }
}
</script>

<style scoped>
.workTime-body {
  background: #f6f6f6;
  border-radius: 10px;
  margin: 0 0 10px 15px;
  box-sizing: border-box;
}

.time-header {
  margin-bottom: 20px;
}

.workTime-body:last-child {
  margin-bottom: 0;
}

.workTime-title {
  padding: 10px 15px;
  border-bottom: 1px solid #f1f1f1;
  color: #333;
  font-weight: 500;
  margin-top: 0;
  display: flex;
  align-items: center;
}

.workTime-title-name {
  flex: 1;
}

.workTime-title-icon {
  font-size: 16px;
  color: #999;
  cursor: pointer;
}

.workTime-time {
  padding: 0 20px 20px;
}

.workTime-time-list {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.workTime-time-list:last-child {
  margin-bottom: 0;
}

.workTime-time-list-l {
  flex: 1;
}

.workTime-time-list-i {
  font-style: normal;
  color: #ed5565;
  border-radius: 5px;
  font-size: 12px;
  border: 1px solid #ed5565;
  padding: 2px 5px;
  margin-left: 10px;
}

.abnormal-time {
  margin-left: 20px;
}

.abnormal-time-list {
  padding: 5px 0;
  color: #000;
}

.abnormal-time-list {
  display: flex;
  align-items: center;
}

.time-box {
  margin: 0 10px;
}

.daytime {
  padding-left: 20px;
}

.daytime-tips {
  color: #000;
}

.daytime-tips em {
  color: red;
}

.daytime-tips span {
  color: #999;
}

.daytime-count {
  color: #000;
}

.rest-time {
  padding-left: 20px;
  color: #000;
  margin-bottom: 10px;
}

.rest-time-list {

}

.rest-time-line {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.rest-time-line:last-child {
  margin-bottom: 0;
}

.rest-time-line .el-icon-circle-close {
  font-size: 18px;
  cursor: pointer;
  color: #999;
}
</style>
